package es.udc.iagolast.androidJam.Util;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;

import es.udc.iagolast.androidJam.Activities.MainMapActivity;
/**
 * LocationListener que cada vez que cambias de localizacion crea un punto
 * @author swicer
 *
 */
public class MyLocationListener implements LocationListener {
	private Context mContext;
	private GeoPoint point;
	private MapController mController;
	private MainMapActivity mAct;

	public MyLocationListener(Context context,MapController mapController,MainMapActivity mapsActivity) {
		mContext = context;
		mController = mapController;
		mAct = mapsActivity;
		
	}

	@Override
	public void onLocationChanged(Location loc) {
		loc.getLatitude();
		loc.getLongitude();
		String coordinates[] = { "" + loc.getLatitude(),
				"" + loc.getLongitude() };
		double lat = Double.parseDouble(coordinates[0]);
		double lng = Double.parseDouble(coordinates[1]);

		point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
		
		mController.animateTo(point);
		mController.setZoom(15);		
		mAct.addPoint(point);
	}
	
	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(mContext, "Gps Disabled", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(mContext, "Gps Enabled", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}
	
	public GeoPoint getPoint(){
		return point;
	}
}
